import MSData
import ReaderMSAPI
import ReaderMSDB
import MSLogger
import PCNDBMethods
import PCNDBFirebird

import time
import os
import sys

if os.path.isfile('C:\\safe\\ms.pid'):
    print 'MS DAEMON already running! Exit now'
    sys.exit()
f = open('C:\\safe\\ms.pid', 'w')
f.write('PID')

MSLogger.logInit('C:\\safe\\ms.new.log', 5, True)
reader = ReaderMSDB.Reader('localhost', '2929', 'PGSMData', 'sa', 'safeinside')
reader.forceConnection()

writer = PCNDBFirebird.Writer('localhost:C:\\pcn6\\data\\DATA.GDB', 'SYSDBA', 'masterkey')
writer.connect()

objects = []
PCNDBMethods.GET_MSObjects_FROM_CarOnline(reader.connection.cursor(), objects)

try:
    while True:
        reader.readEvents_PGSM(objects)
        reader.readEvents_ContactId(objects)
        writer.writeEvents_Static(objects)
        time.sleep(5)
except:
    os.unlink('C:\\safe\\ms.pid')

